home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / misc / math / libalgo.lha / algomath / src / hail.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-30  |  911 b   |  53 lines

  1. /*investigate hailstone numbers:
  2.     if n == odd then multiply it by 3 and add 1
  3.     if n == even  divide it by 2
  4.     call this function until it return 1.
  5.     if n is negative <0 then the result is also negative.
  6.     if the result is bigger than int or n=0, it return 0 (failure).
  7.     n=77671 is the first number that return 0.
  8.     if you want to investigate all big numbers,I recommend you to use the
  9.     c/c++ miracl library:
  10.     ftp://ftp.compapp.dcu.ie/pub/crypto/miracl.zip
  11.  
  12.     ex.:
  13.     
  14.     n=25267;
  15.     while(n!=1){
  16.         n=am_hailstone(n);
  17.             if(n==0){
  18.                 printf("n is to big\n");
  19.                 exit();
  20.             }
  21.         printf("%d\n",n);
  22.     }
  23.     
  24. */
  25.  
  26.  
  27. #include "defs.h"
  28.  
  29.  
  30. int am_hailstone(int n){
  31.  
  32.     if(n>0){
  33.         if(n==1)
  34.             return 1;
  35.         if(n>715827882)
  36.             return 0;
  37.          if (n&1)
  38.             return n*3+1;
  39.         return n/2;    
  40.     }
  41.     else if(n<0){
  42.         n=-n;
  43.         if(n==1)
  44.             return 1;
  45.          if(n>715827882)
  46.             return 0;
  47.          if (n&1)
  48.             return -(n*3+1);
  49.         return -(n/2);    
  50.     }
  51.     else
  52.         return 0;
  53. }